<template>
  <div>
    <!-- 编辑弹窗 -->
    <cvue-dialog
      :dialogVisible="dialogVisible"
      :title="title"
      :dialogLoading="dialogLoading"
      dialogWidth="500px"
      @handleOpen="getDetail"
      @closeDialog="closeDialog"
      @confirmDialog="confirmDialog"
      @handleClose="handleClose">
      <div class="dialog-body" slot="dialogBody">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="用户名" prop="username">
                <el-input v-model.trim="ruleForm.username" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="昵称" prop="nickname">
                <el-input v-model.trim="ruleForm.nickname" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="手机号" prop="phone">
                <el-input v-model.trim="ruleForm.phone" :disabled="disabled"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="ruleForm.sex">
                  <el-radio label="1">男</el-radio>
                  <el-radio label="0">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="ruleForm.status">
                  <el-radio :label="0">无效</el-radio>
                  <el-radio :label="1">有效</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="角色" prop="userRole">
                <el-select v-model="ruleForm.userRole" multiple placeholder="请选择" style="width:100%" clearable>
                  <el-option
                    v-for="item in roles"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </cvue-dialog>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import cvueDialog from '@/components/cvue-dialog'
import { validPhone, validIp, validIdCN } from '@/util/validator.js'
export default {
  name: 'userEdit',
  components: {
    cvueDialog
  },
  data () {
    return {
      ruleForm: {
        username: '',
        nickname: '',
        phone: '',
        sex: '1',
        status: 1,
        tenantId: '',
        userRole: []
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
          { validator: validIdCN, trigger: 'blur' }
        ],
        nickname: [
          // { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: validPhone, trigger: 'blur' }
        ],
        tenantId: [
          { required: true, message: '请选择租户', trigger: 'blur' }
        ],
        userRole: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ]
      },
      disabled: false,
      dialogLoading: false,
      status: 1,
      roles: [],
      tenantArr: []
    }
  },
  mounted () {
    this.getEditDetail()
    this.getRoles()
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    title () {
      var title = ''
      if (this.$parent.actionType == 'add') {
        title = '新增'
      } else {
        title = '编辑'
      }
      return title
    },
    ...mapGetters(['token', 'userInfo', 'tenantId'])
  },
  methods: {
    // 触发事件
    handleClose () {
      this.$emit('handleClose')
    },
    closeDialog () {
      this.$emit('closeDialog')
    },
    confirmDialog () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          var params = {
            id: this.$parent.actionType == 'add' ? '' : this.$parent.rowData.id,
            name: this.ruleForm.nickname,
            mobile: this.ruleForm.phone,
            role_id: this.ruleForm.userRole.join(','),
            sex: this.ruleForm.sex,
            username: this.ruleForm.username,
            status: this.ruleForm.status
          }
          // console.log(params)
          this.dialogLoading = true
          var url = this.$parent.actionType == 'add' ? 'manage/UserPost' : 'manage/UserPut'
          this.$store.dispatch(url, params).then(res => {
            // console.log(res)
            this.$message({
              message: res.msg,
              duration: 2000,
              type: 'success'
            })
            this.$emit('confirmDialog')
            this.dialogLoading = false
          }).catch(err => {
            this.$message({ message: err.msg, duration: 2000, type: 'error' })
            this.dialogLoading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getDetail () {
    },
    getRoles (tenantId) {
      var params = {}
      this.dialogLoading = true
      this.$store.dispatch('manage/RoleGetAll', params).then(res => {
        this.roles = res.data
        this.dialogLoading = false
      }).catch(err => {
        this.$message({ message: err.msg, duration: 2000, type: 'error' })
        this.dialogLoading = false
      })
    },
    getEditDetail () {
      // console.log(this.$parent.rowData)
      if (this.$parent.actionType == 'edit') {
        this.$set(this.ruleForm, 'username', this.$parent.rowData.username)
        this.$set(this.ruleForm, 'nickname', this.$parent.rowData.name)
        this.$set(this.ruleForm, 'phone', this.$parent.rowData.mobile)
        this.$set(this.ruleForm, 'sex', this.$parent.rowData.sex)
        this.$set(this.ruleForm, 'userRole', this.$parent.rowData.role_id.split(','))
        this.$set(this.ruleForm, 'status', this.$parent.rowData.status)
      }
    }
  }
}
</script>
<style scoped>
</style>
